package org.yecq.tradedata.save;

import com.jhhc.baseframework.data.record.SqlOperator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.yecq.tradedata.bean.FuturesDayKLineBean;

/**
 * 期货数据保存
 *
 * @author yecq
 */
@Component
public class FuturesSaver {

    private static Logger log = Logger.getLogger(FuturesSaver.class);

    @Autowired
    private SqlOperator sql;

    // 保存期货合约数据
    public void saveContract(String code, String name, String exchange) {
        // 看是否存在
        if (isCodeExist(code) != null) {
            throw new IllegalStateException("合约" + code + "已存在");
        }
        String stmt = "insert into futures(code,name,exchg_id) value(?,?,(select id from exchg where name=?))";
        this.sql.insert(stmt, code, name, exchange);
    }

    // 保存或添加期货合约
    public void saveOrUpdateContract(String code, String name, String exchg) {
        // 看有无该品种
        String id = isCodeExist(code);
        if (id == null) {
            // 插入该品种
            this.sql.insert("insert into futures(code,name,exchg_id) value(?,?,(select id from exchg where name=? and type='期货'))", code, name, exchg);
            log.info("已添加品种" + code + " ...");
        } else {
            // 修改该品种
            this.sql.update("update futures set code=?,name=?,exchg_id=(select id from exchg where name=? and type='期货') where id=?", code, name, exchg, id);
            log.info("已修改品种" + code + " ...");
        }
    }

    private String isCodeExist(String code) {
        List<Map<String, Object>> list = this.sql.query("select * from futures where code=?", code);
        return list.isEmpty() ? null : list.get(0).get("id") + "";
    }

    // 保存一系列数据
    public void saveOrUpdateList(List<FuturesDayKLineBean> list) {
        for (FuturesDayKLineBean bean : list) {
            saveOrUpdateSingle(bean);
        }
    }

    // 保存并更新单个数据，
    public void saveOrUpdateSingle(FuturesDayKLineBean bean) {
        // 检查当天的数据是否已有
        List<Map<String, Object>> li = this.sql.query("select * from futures_day_kline where date=? "
                + "and futures_id = (select id from futures where code=?)", bean.getDate(), bean.getCode());
        if (!li.isEmpty()) {
            String id = li.get(0).get("id") + "";
            this.sql.update("update futures_day_kline set open=?,close=?,high=?,low=?,vol=?,date=?,futures_id=(select id from futures where code=?) where id=?", bean.getOpen(), bean.getClose(), bean.getHigh(),
                    bean.getLow(), bean.getVol(), bean.getDate(), bean.getCode(), id);
            log.info("已修改" + bean.getCode() + "的交易数据 " + bean.getDate() + " ...");
        } else {
            String stmt = "insert into futures_day_kline(open,close,high,low,vol,date,futures_id) "
                    + "value(?,?,?,?,?,?,(select id from futures where code=?))";
            this.sql.insert(stmt, bean.getOpen(), bean.getClose(), bean.getHigh(), bean.getLow(), bean.getVol(), bean.getDate(), bean.getCode());
            log.info("已保存" + bean.getCode() + "的交易数据 " + bean.getDate() + " ...");
        }

    }

    // 去除已有的非交易日的数据
    public int clearDataOfExceptionDate() {
        List<Map<String, Object>> list = this.sql.query("select * from futures_day_kline where date in (select date from exception_date)");
        for (Map<String, Object> item : list) {
            String id = item.get("id") + "";
            this.sql.delete("delete from futures_day_kline where id = ?", id);
        }
        return list.size();
    }
}
